package com.jckj.core.aop;

import com.jckj.core.annotation.CurrentDataSource;
import com.jckj.core.db.DynamicDataSource;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * Created by zhiyuan on 2017/12/23.
 */
@Component
@Aspect
public class DataSourceAop {

    private Logger logger = LoggerFactory.getLogger(DataSourceAop.class);

    @Before("execution(* com.jckj.*.service.impl.*Impl.*(..)) && @annotation(currentDataSource)")
    public void setDataSource(CurrentDataSource currentDataSource){
        logger.info("开始设置数据源");
        logger.info("本次操作使用的数据源为：" + currentDataSource.value().getName());
        DynamicDataSource.setDataSource(currentDataSource.value().getName());
        logger.info("设置结束");
    }
}
